#!/bin/bash

SWNAME="com.qihoo.360zip"
SWVER=1.0.0.1012

# 注意安装顺序，后装者依赖前装者
DEB_PATH1=./downloads/${SWNAME}.deb

DIR_DESKTOP_FILES=/usr/share/applications
DSK_FILE=${SWNAME}.desktop
DSK_PATH=${DIR_DESKTOP_FILES}/${DSK_FILE}
app_dir="/opt/apps/com.qihoo.360zip"

action=$1
if [ "$action" == "" ]; then action=安装; fi

. ./scripts/common.sh

function sw_download() {
	tmpdns=`cd /exbin && droidexec ./vm_getHostByName.sh ${GITHUB_PROXY_HOST}`
	exit_if_fail $? "DNS解析失败"
	echo "$tmpdns" >> /etc/hosts

	case "${CURRENT_VM_ARCH}" in
		"arm64")
			swUrl="https://mirrors.sdu.edu.cn/spark-store-repository/aarch64-store/tools/com.qihoo.360zip/com.qihoo.360zip_${SWVER}_arm64.deb"
			download_file2 "${DEB_PATH1}" "${swUrl}"
			exit_if_fail $? "下载失败，网址：${swUrl}"
		;;
		*) exit_unsupport ;;
	esac
}

function sw_install() {

	mkdir -p ${app_dir}

	install_deb ${DEB_PATH1}
	exit_if_fail $? "安装失败，软件包：${DEB_PATH1}"
}

function sw_create_desktop_file() {
	echo "正在生成桌面文件"

	tmpfile="${app_dir}/entries/applications/com.qihoo.360zip.desktop"
	cp -f ${tmpfile} ${DIR_DESKTOP_FILES}/
	exit_if_fail $? "安装失败，无法复制启动图标"

	cp2desktop ${tmpfile}

	gxmessage -title "提示"     $'\n安装完成\n\n'  -center
}

if [ "${action}" == "卸载" ]; then
	# echo "暂不支持卸载"
	# exit 1

	PKGName=${SWNAME}
	apt-get purge ${PKGName} -y
	apt-get -y autoremove --purge ${PKGName}

	rm -rf ${DEB_PATH1}

	rm -rf /usr/bin/${SWNAME}

	rm -rf /opt/apps/${SWNAME}

	rm2desktop ${SWNAME}.desktop

	apt-get clean
else
	sw_download
	sw_install
	sw_create_desktop_file
fi

